package 
{
	import controller.Controller;
	import controller.PresetControl;
	import display.DisplayPresetButton;
	import flash.display.StageDisplayState;
	import flash.events.TimerEvent;
	import flash.system.System;
	import flash.utils.ByteArray;
	import flash.utils.Timer;
	import utils.Fonts;
	
	import display.DisplayBackGround;
	import display.DisplayScrollPane;
	import display.DisplayTabBtn;
	import display.DisplayPlayer;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	
	import data.PresetData;
	
	import utils.XMLimport;
	
	/**
	 * ...
	 * @author hrh - 2011
	 
	 */
	
	public class Shell extends MovieClip
	{
		//embed xml
		
		[Embed(source='../xml/preset.xml',mimeType="application/octet-stream")]
		public static const Preset:Class; 
		
		[Embed(source='../xml/button.xml',mimeType="application/octet-stream")]
		public static const fButton:Class; 
		
		//data
		
		private var presetData:Array;
		private var data:Array;
		private var presetItem:PresetData;
		
		// controllers
		
		private var control:Controller;
		private var presetControl:PresetControl;
		
		// movieclips
		
		private var scrollPane:DisplayScrollPane;
		private var player:DisplayPlayer;
		
		// xml
		
		private var xmlButtonImport:XMLimport;
		private var xmlPresetImport:XMLimport;
		
		public function Shell()
		{
			this.stage.scaleMode = 'noScale';
			this.stage.align = 'TL';
			
			ini();
		}
		
		private function ini():void
		{
			var filePreset:ByteArray = new Preset;
			var strPreset:String = filePreset.readUTFBytes( filePreset.length );
			var xmlPreset:XML = new XML( strPreset );
			
			xmlPresetImport = new XMLimport;
			xmlPresetImport.addEventListener(Event.COMPLETE, onPresetImportComplete);
			
			xmlPresetImport.load('../xml/preset.xml', 'preset', xmlPreset, true);
		}
		
		private function onPresetImportComplete(e:Event):void
		{
			var fileButton:ByteArray = new fButton;
			var strButton:String = fileButton.readUTFBytes( fileButton.length );
			var xmlButton:XML = new XML( strButton );
			
			presetData = xmlPresetImport.data;
			
			xmlButtonImport = new XMLimport;
			xmlButtonImport.addEventListener(Event.COMPLETE, onButtonImportComplete);
			
			xmlButtonImport.load('../xml/button.xml','button',xmlButton,true);
		}
		
		private function onButtonImportComplete(e:Event):void
		{
			data = xmlButtonImport.data;
			
			build();
		}
		
		private function build():void
		{
			control = new Controller(this);
			presetControl = new PresetControl(control, data, presetData);
			
			scrollPane = new DisplayScrollPane(this, control, data, presetItem);
			player = new DisplayPlayer(this,control,data);
			
			control.start(scrollPane, player, presetControl);
			
			// set first preset
			presetControl.upDate(presetData[0]);
			
			addChild(player);
			addChild(scrollPane);
		}
	}
}